﻿using IOP.SgrA;
using IOP.SgrA.ECS;
using IOP.SgrA.Input;
using System;
using System.Collections.Generic;
using System.Numerics;
using System.Text;

namespace VkSample611
{
    public class TransformSystem : ComponentSystem
    {

        private float currLodLevel = 0;
        const float SPAN = 5;
        float startX = -SPAN; //x 坐标初始值
        float startY = SPAN; //y 坐标初始值

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="contextManager"></param>
        public TransformSystem()
        {
        }

        public override void Initialization()
        {
            float x = 2.4f;
            int index = 0;
            ContextManager.Foreach<Group>((group) =>
            {
                Context context = ContextManager.GetContext(group.Entity);
                context.SetCustomPropery<float>("lod", currLodLevel);
                group.Transform.Position = new Vector3(startX + SPAN * x, startY - 1 * SPAN, 0);
                if(index == 1)
                {
                    group.Transform.Angle = 90;
                    x -= 0.7f;
                }
                else if (index == 2)
                {
                    group.Transform.Angle = 90;
                    x -= 1.05f;
                }
                else
                {
                    group.Transform.Angle = 0;
                    x -= 1.05f;
                }
                index++;
                return group;
            });
        }

        public override void Update(TimeSpan timeSpan)
        {
            ContextManager.Foreach<Group>((group) =>
            {
                Context context = ContextManager.GetContext(group.Entity);
                ref MVPMatrix mvp = ref context.GetMVPMatrix();
                mvp.ModelMatrix = Matrix4x4.CreateRotationY(group.Transform.Angle / 180.0f * MathF.PI)
                * Matrix4x4.CreateTranslation(group.Transform.Position);
                context.PushToRenderGroup();
            });
        }

        public struct Group
        {
            public Entity Entity;
            public TransformComponent Transform;
        }
    }
}
